package com.tz.crm.service.impl;


import com.tz.crm.dao.ContactDao;
import com.tz.crm.entity.Contact;
import com.tz.crm.entity.User;
import com.tz.crm.entity.enumeration.Status;
import com.tz.crm.service.ContactService;
import com.tz.crm.utils.Pager;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 联系人服务类
 */
@Service
@Transactional
@SuppressWarnings("unchecked")
public class ContactServiceImpl extends BaseServiceImpl<Contact, String>
        implements ContactService {

    @Autowired
    private ContactDao contactDao;

    /**
     * 有参构造
     *
     * @param contactDao 注入的 ContactDao 组件
     */
    @Autowired
    public ContactServiceImpl(ContactDao contactDao) {
        this.contactDao = contactDao;
        super.setBaseDao(contactDao);
    }

    @Override
    public List<Contact> getByCustomerId(String id) {
        return contactDao.getByCustomerId(id);
    }

    @Override
    public Contact getMajorByCustomerId(String id) {
        return contactDao.getMajorByCustomerId(id);
    }

    @Override
    public Pager<Contact> getByPager(Pager pager, Contact contact) {
        DetachedCriteria dc = DetachedCriteria.forClass(Contact.class);
        dc.add(Restrictions.eq("major", true));

        if (StringUtils.isNotEmpty(contact.getPhone())) {
            dc.add(Restrictions.like("phone", "%" + contact.getPhone().trim() + "%"));
        }
        if (StringUtils.isNotEmpty(contact.getMobilePhone())) {
            dc.add(Restrictions.like("mobilePhone", "%" + contact.getMobilePhone().trim() + "%"));
        }
        if (StringUtils.isNotEmpty(contact.getName())) {
            dc.add(Restrictions.like("name", "%" + contact.getName().trim() + "%"));
        }


        if (contact.getCustomer() != null) {
            DetachedCriteria criteriaCustomer = dc.createCriteria("customer");
            if (StringUtils.isNotEmpty(contact.getCustomer().getName())) {
                criteriaCustomer.add(Restrictions.like("name", "%" + contact.getCustomer().getName().trim() + "%"));
            }
            if (StringUtils.isNotEmpty(contact.getCustomer().getProvinceName())) {
                criteriaCustomer.add(Restrictions.eq("provinceName", contact.getCustomer().getProvinceName()));
            }
            if (StringUtils.isNotEmpty(contact.getCustomer().getCityName())) {
                criteriaCustomer.add(Restrictions.eq("cityName", contact.getCustomer().getCityName()));
            }
            if (StringUtils.isNotEmpty(contact.getCustomer().getAddress())) {
                criteriaCustomer.add(Restrictions.like("address", "%" + contact.getCustomer().getAddress().trim() + "%"));
            }
        }
        return contactDao.findByPager(pager, dc);
    }

    @Override
    public Pager getServeByUser(Pager pager, User owner) {
        DetachedCriteria dc = DetachedCriteria.forClass(Contact.class);
        dc.add(Restrictions.eq("major", true));//设置主联系人
        DetachedCriteria criteriaCustomer = dc.createCriteria("customer");
        criteriaCustomer.add(Restrictions.eq("status", Status.SIGNED));//设置客户状态
        criteriaCustomer.add(Restrictions.eq("owner", owner));//设置客户专员
        return contactDao.findByPager(pager, dc);
    }
}